package com.huike.web.controller.review;


import com.github.pagehelper.PageHelper;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import org.apache.commons.collections4.Get;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**
     * =========================================================新增数据============================================
     */
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult saveAll(@PathVariable String name, @PathVariable String age, @PathVariable String sex) {
        Review review = new Review();
        review.setName(name);
        review.setAge(Integer.valueOf(age));
        review.setSex(sex);

        int result = reviewService.addAll(review);
        return new AjaxResult(200, "成功插入：" + result + "条数据");
    }

    @PostMapping("/saveData")
    public AjaxResult saveAll(@RequestBody Review review) {
        int result = reviewService.addAll(review);
        return new AjaxResult(200, "成功插入：" + result + "条数据");
    }

    /**
     * =========================================================删除数据=============================================
     */
    @DeleteMapping("/remove/{id}")
    public AjaxResult deleteById(@PathVariable Long id) {
        int result = reviewService.deleteById(id);
        return new AjaxResult(200, "成功删除：" + result + "条数据");
    }

    /**
     * =========================================================修改数据=============================================
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody Review review) {
        reviewService.update(review);
        return new AjaxResult(200, "修改成功");
    }

    /**
     * =========================================================查询数据=============================================
     */
    @GetMapping("/getById")
    public AjaxResult selectById(Long id) {
        List<Review> reviews = reviewService.selectById(id);
        return new AjaxResult(200, "操作成功", reviews);
    }

    @GetMapping("/getDataByPage")
    public TableDataInfo selectByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Review> reviews = reviewService.selectAll();
        return new TableDataInfo(reviews, reviews.size());
    }
}